package com.suanfa.sort.str;

/**
 * @author jukg
 * @date 2025/01/07 10:40
 * @description
 */
public class LongestCommonPrefix {

    public static void main(String[] args) {
        String[] strs = {"ab", "a"};
        System.out.println(longestCommonPrefix(strs));

    }

    public static String longestCommonPrefix(String[] strs) {
        if(strs.length < 1){
            return "";
        }
        if(strs.length < 2){
            return strs[0];
        }

        String str = strs[0];
        StringBuffer sb = new StringBuffer();
        for(int i =0; i<str.length(); i++){
            char sub = str.charAt(i);
            for(int j = 1; j<strs.length; j++ ){
                if(i > strs[j].length()-1){
                    return sb.toString();
                }
                if(strs[j].charAt(i)!= sub){
                    return sb.toString();
                }
            }
            sb.append(sub);
        }
        return sb.toString();
    }

}
